home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / selectmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  12.2 KB  |  507 lines

  1. /* select - Module containing unix select(2) call.
  2.    Under Unix, the file descriptors are small integers.
  3.    Under Win32, select only exists for sockets, and sockets may
  4.    have any value except INVALID_SOCKET.
  5.    Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
  6.    >= 0.
  7. */
  8.  
  9. #include "Python.h"
  10.  
  11. #ifdef HAVE_UNISTD_H
  12. #include <unistd.h>
  13. #endif
  14.  
  15. #ifdef __sgi
  16. /* This is missing from unistd.h */
  17. extern void bzero();
  18. #endif
  19.  
  20. #ifndef DONT_HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23.  
  24. #if defined(PYOS_OS2)
  25. #include <sys/time.h>
  26. #include <utils.h>
  27. #endif
  28.  
  29. #ifdef MS_WINDOWS
  30. #include <winsock.h>
  31. #else
  32. #ifdef __BEOS__
  33. #include <net/socket.h>
  34. #define SOCKET int
  35. #else
  36. #include "myselect.h" /* Also includes mytime.h */
  37. #define SOCKET int
  38. #endif
  39. #endif
  40.  
  41. static PyObject *SelectError;
  42.  
  43. /* list of Python objects and their file descriptor */
  44. typedef struct {
  45.     PyObject *obj;                 /* owned reference */
  46.     SOCKET fd;
  47.     int sentinel;                 /* -1 == sentinel */
  48. } pylist;
  49.  
  50. #include "protos/selectmodule.h"
  51.  
  52. static void
  53. reap_obj(fd2obj)
  54.     pylist fd2obj[FD_SETSIZE + 3];
  55. {
  56.     int i;
  57.     for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) {
  58.         Py_XDECREF(fd2obj[i].obj);
  59.         fd2obj[i].obj = NULL;
  60.     }
  61.     fd2obj[0].sentinel = -1;
  62. }
  63.  
  64.  
  65. /* returns -1 and sets the Python exception if an error occurred, otherwise
  66.    returns a number >= 0
  67. */
  68. static int
  69. list2set(list, set, fd2obj)
  70.     PyObject *list;
  71.     fd_set *set;
  72.     pylist fd2obj[FD_SETSIZE + 3];
  73. {
  74.     int i;
  75.     int max = -1;
  76.     int index = 0;
  77.     int len = PyList_Size(list);
  78.     PyObject* o = NULL;
  79.  
  80.     fd2obj[0].obj = (PyObject*)0;         /* set list to zero size */
  81.     FD_ZERO(set);
  82.  
  83.     for (i = 0; i < len; i++)  {
  84.         PyObject *meth;
  85.         SOCKET v;
  86.  
  87.         /* any intervening fileno() calls could decr this refcnt */
  88.         if (!(o = PyList_GetItem(list, i)))
  89.                     return -1;
  90.  
  91.         Py_INCREF(o);
  92.  
  93.         if (PyInt_Check(o)) {
  94.             v = PyInt_AsLong(o);
  95.         }
  96.         else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
  97.         {
  98.             PyObject *fno = PyEval_CallObject(meth, NULL);
  99.             Py_DECREF(meth);
  100.             if (fno == NULL)
  101.                 goto finally;
  102.  
  103.                         if (!PyInt_Check(fno)) {
  104.                 PyErr_SetString(PyExc_TypeError,
  105.                                        "fileno method returned a non-integer");
  106.                 Py_DECREF(fno);
  107.                 goto finally;
  108.                         }
  109.                         v = PyInt_AsLong(fno);
  110.             Py_DECREF(fno);
  111.         }
  112.         else {
  113.             PyErr_SetString(PyExc_TypeError,
  114.             "argument must be an int, or have a fileno() method.");
  115.             goto finally;
  116.         }
  117. #if defined(_MSC_VER)
  118.         max = 0;             /* not used for Win32 */
  119. #else  /* !_MSC_VER */
  120.         if (v < 0 || v >= FD_SETSIZE) {
  121.             PyErr_SetString(PyExc_ValueError,
  122.                     "filedescriptor out of range in select()");
  123.             goto finally;
  124.         }
  125.         if (v > max)
  126.             max = v;
  127. #endif /* _MSC_VER */
  128.         FD_SET(v, set);
  129.  
  130.         /* add object and its file descriptor to the list */
  131.         if (index >= FD_SETSIZE) {
  132.             PyErr_SetString(PyExc_ValueError,
  133.                       "too many file descriptors in select()");
  134.             goto finally;
  135.         }
  136.         fd2obj[index].obj = o;
  137.         fd2obj[index].fd = v;
  138.         fd2obj[index].sentinel = 0;
  139.         fd2obj[++index].sentinel = -1;
  140.     }
  141.     return max+1;
  142.  
  143.   finally:
  144.     Py_XDECREF(o);
  145.     return -1;
  146. }
  147.  
  148. /* returns NULL and sets the Python exception if an error occurred */
  149. static PyObject *
  150. set2list(set, fd2obj)
  151.     fd_set *set;
  152.     pylist fd2obj[FD_SETSIZE + 3];
  153. {
  154.     int i, j, count=0;
  155.     PyObject *list, *o;
  156.     SOCKET fd;
  157.  
  158.     for (j = 0; fd2obj[j].sentinel >= 0; j++) {
  159.         if (FD_ISSET(fd2obj[j].fd, set))
  160.             count++;
  161.     }
  162.     list = PyList_New(count);
  163.     if (!list)
  164.         return NULL;
  165.  
  166.     i = 0;
  167.     for (j = 0; fd2obj[j].sentinel >= 0; j++) {
  168.         fd = fd2obj[j].fd;
  169.         if (FD_ISSET(fd, set)) {
  170. #ifndef _MSC_VER
  171.             if (fd > FD_SETSIZE) {
  172.                 PyErr_SetString(PyExc_SystemError,
  173.                "filedescriptor out of range returned in select()");
  174.                 goto finally;
  175.             }
  176. #endif
  177.             o = fd2obj[j].obj;
  178.             fd2obj[j].obj = NULL;
  179.             /* transfer ownership */
  180.             if (PyList_SetItem(list, i, o) < 0)
  181.                 goto finally;
  182.  
  183.             i++;
  184.         }
  185.     }
  186.     return list;
  187.   finally:
  188.     Py_DECREF(list);
  189.     return NULL;
  190. }
  191.  
  192.     
  193. #if defined(AMITCP) || defined(INET225)
  194. /* Amiga's version of select: WaitSelect()/selectwait() support */
  195. /* (additional 5th parameter: signal waitmask) */
  196. static PyObject *
  197. select_select(self, args)
  198.     PyObject *self;
  199.     PyObject *args;
  200. {
  201. #if defined (MS_WINDOWS) || defined (_AMIGA)
  202.     /* This would be an awful lot of stack space on Windows! */
  203.     pylist *rfd2obj, *wfd2obj, *efd2obj;
  204. #else
  205.     pylist rfd2obj[FD_SETSIZE + 3];
  206.     pylist wfd2obj[FD_SETSIZE + 3];
  207.     pylist efd2obj[FD_SETSIZE + 3];
  208. #endif
  209.     PyObject *ifdlist, *ofdlist, *efdlist;
  210.     PyObject *ret = NULL;
  211.     PyObject *tout = Py_None;
  212.     fd_set ifdset, ofdset, efdset;
  213.     double timeout;
  214.     struct timeval tv, *tvp;
  215.     int seconds;
  216.     int imax, omax, emax, max;
  217.     int n;
  218.     ULONG waitmask=0;
  219.     BOOL do_waitmask = FALSE;
  220.  
  221.     /* convert arguments */
  222.     if (!PyArg_ParseTuple(args, "OOO|Oi",
  223.                   &ifdlist, &ofdlist, &efdlist, &tout, &waitmask))
  224.         return NULL;
  225.  
  226.     if (tout == Py_None)
  227.         tvp = (struct timeval *)0;
  228.     else if (!PyArg_Parse(tout, "d", &timeout)) {
  229.         PyErr_SetString(PyExc_TypeError,
  230.                 "timeout must be a float or None");
  231.         return NULL;
  232.     }
  233.     else {
  234.         seconds = (int)timeout;
  235.         timeout = timeout - (double)seconds;
  236.         tv.tv_sec = seconds;
  237.         tv.tv_usec = (int)(timeout*1000000.0);
  238.         tvp = &tv;
  239.     }
  240.  
  241.     if(waitmask) do_waitmask=TRUE;
  242.  
  243.     /* sanity check first three arguments */
  244.     if (!PyList_Check(ifdlist) ||
  245.         !PyList_Check(ofdlist) ||
  246.         !PyList_Check(efdlist))
  247.     {
  248.         PyErr_SetString(PyExc_TypeError,
  249.                 "arguments 1-3 must be lists");
  250.         return NULL;
  251.     }
  252.  
  253. #if defined(MS_WINDOWS) || defined (_AMIGA)
  254.     /* Allocate memory for the lists */
  255.     rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  256.     wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  257.     efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  258.     if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
  259.         if (rfd2obj) PyMem_DEL(rfd2obj);
  260.         if (wfd2obj) PyMem_DEL(wfd2obj);
  261.         if (efd2obj) PyMem_DEL(efd2obj);
  262.         return NULL;
  263.     }
  264. #endif
  265.     /* Convert lists to fd_sets, and get maximum fd number
  266.      * propagates the Python exception set in list2set()
  267.      */
  268.     rfd2obj[0].sentinel = -1;
  269.     wfd2obj[0].sentinel = -1;
  270.     efd2obj[0].sentinel = -1;
  271.     if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) 
  272.         goto finally;
  273.     if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) 
  274.         goto finally;
  275.     if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) 
  276.         goto finally;
  277.     max = imax;
  278.     if (omax > max) max = omax;
  279.     if (emax > max) max = emax;
  280.  
  281.     Py_BEGIN_ALLOW_THREADS
  282. #ifdef AMITCP
  283.     n = WaitSelect(max, &ifdset, &ofdset, &efdset, tvp, &waitmask);
  284. #else /* INET225 */
  285.     n = selectwait(max, &ifdset, &ofdset, &efdset, tvp, &waitmask);
  286. #endif    
  287.     Py_END_ALLOW_THREADS
  288.  
  289.     if (n < 0) {
  290.         PyErr_SetFromErrno(SelectError);
  291.     }
  292.     else if (n == 0) {
  293.                 /* optimization */
  294.         ifdlist = PyList_New(0);
  295.         if (ifdlist) {
  296.             if(do_waitmask) ret=Py_BuildValue("OOOi", ifdlist, ifdlist, ifdlist, waitmask);
  297.             else ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
  298.             Py_DECREF(ifdlist);
  299.         }
  300.     }
  301.     else {
  302.         /* any of these three calls can raise an exception.  it's more
  303.            convenient to test for this after all three calls... but
  304.            is that acceptable?
  305.         */
  306.         ifdlist = set2list(&ifdset, rfd2obj);
  307.         ofdlist = set2list(&ofdset, wfd2obj);
  308.         efdlist = set2list(&efdset, efd2obj);
  309.         if (PyErr_Occurred())
  310.             ret = NULL;
  311.         else
  312.         {
  313.             if(do_waitmask) ret=Py_BuildValue("OOOi", ifdlist, ofdlist, efdlist, waitmask);
  314.             else ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
  315.         }
  316.  
  317.         Py_DECREF(ifdlist);
  318.         Py_DECREF(ofdlist);
  319.         Py_DECREF(efdlist);
  320.     }
  321.     
  322.   finally:
  323.     reap_obj(rfd2obj);
  324.     reap_obj(wfd2obj);
  325.     reap_obj(efd2obj);
  326. #if defined(MS_WINDOWS) || defined (_AMIGA)
  327.     PyMem_DEL(rfd2obj);
  328.     PyMem_DEL(wfd2obj);
  329.     PyMem_DEL(efd2obj);
  330. #endif
  331.     return ret;
  332. }
  333.  
  334. #else /* ! AMITCP || INET225 */
  335. /* This is the select function for all other platforms */
  336. static PyObject *
  337. select_select(self, args)
  338.     PyObject *self;
  339.     PyObject *args;
  340. {
  341. #ifdef MS_WINDOWS
  342.     /* This would be an awful lot of stack space on Windows! */
  343.     pylist *rfd2obj, *wfd2obj, *efd2obj;
  344. #else
  345.     pylist rfd2obj[FD_SETSIZE + 3];
  346.     pylist wfd2obj[FD_SETSIZE + 3];
  347.     pylist efd2obj[FD_SETSIZE + 3];
  348. #endif
  349.     PyObject *ifdlist, *ofdlist, *efdlist;
  350.     PyObject *ret = NULL;
  351.     PyObject *tout = Py_None;
  352.     fd_set ifdset, ofdset, efdset;
  353.     double timeout;
  354.     struct timeval tv, *tvp;
  355.     int seconds;
  356.     int imax, omax, emax, max;
  357.     int n;
  358.  
  359.     /* convert arguments */
  360.     if (!PyArg_ParseTuple(args, "OOO|O:select",
  361.                   &ifdlist, &ofdlist, &efdlist, &tout))
  362.         return NULL;
  363.  
  364.     if (tout == Py_None)
  365.         tvp = (struct timeval *)0;
  366.     else if (!PyArg_Parse(tout, "d", &timeout)) {
  367.         PyErr_SetString(PyExc_TypeError,
  368.                 "timeout must be a float or None");
  369.         return NULL;
  370.     }
  371.     else {
  372.         seconds = (int)timeout;
  373.         timeout = timeout - (double)seconds;
  374.         tv.tv_sec = seconds;
  375.         tv.tv_usec = (int)(timeout*1000000.0);
  376.         tvp = &tv;
  377.     }
  378.  
  379.     /* sanity check first three arguments */
  380.     if (!PyList_Check(ifdlist) ||
  381.         !PyList_Check(ofdlist) ||
  382.         !PyList_Check(efdlist))
  383.     {
  384.         PyErr_SetString(PyExc_TypeError,
  385.                 "arguments 1-3 must be lists");
  386.         return NULL;
  387.     }
  388.  
  389. #ifdef MS_WINDOWS
  390.     /* Allocate memory for the lists */
  391.     rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  392.     wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  393.     efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  394.     if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
  395.         if (rfd2obj) PyMem_DEL(rfd2obj);
  396.         if (wfd2obj) PyMem_DEL(wfd2obj);
  397.         if (efd2obj) PyMem_DEL(efd2obj);
  398.         return NULL;
  399.     }
  400. #endif
  401.     /* Convert lists to fd_sets, and get maximum fd number
  402.      * propagates the Python exception set in list2set()
  403.      */
  404.     rfd2obj[0].sentinel = -1;
  405.     wfd2obj[0].sentinel = -1;
  406.     efd2obj[0].sentinel = -1;
  407.     if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) 
  408.         goto finally;
  409.     if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) 
  410.         goto finally;
  411.     if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) 
  412.         goto finally;
  413.     max = imax;
  414.     if (omax > max) max = omax;
  415.     if (emax > max) max = emax;
  416.  
  417.     Py_BEGIN_ALLOW_THREADS
  418.     n = select(max, &ifdset, &ofdset, &efdset, tvp);
  419.     Py_END_ALLOW_THREADS
  420.  
  421.     if (n < 0) {
  422.         PyErr_SetFromErrno(SelectError);
  423.     }
  424.     else if (n == 0) {
  425.                 /* optimization */
  426.         ifdlist = PyList_New(0);
  427.         if (ifdlist) {
  428.             ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
  429.             Py_DECREF(ifdlist);
  430.         }
  431.     }
  432.     else {
  433.         /* any of these three calls can raise an exception.  it's more
  434.            convenient to test for this after all three calls... but
  435.            is that acceptable?
  436.         */
  437.         ifdlist = set2list(&ifdset, rfd2obj);
  438.         ofdlist = set2list(&ofdset, wfd2obj);
  439.         efdlist = set2list(&efdset, efd2obj);
  440.         if (PyErr_Occurred())
  441.             ret = NULL;
  442.         else
  443.             ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
  444.  
  445.         Py_DECREF(ifdlist);
  446.         Py_DECREF(ofdlist);
  447.         Py_DECREF(efdlist);
  448.     }
  449.     
  450.   finally:
  451.     reap_obj(rfd2obj);
  452.     reap_obj(wfd2obj);
  453.     reap_obj(efd2obj);
  454. #ifdef MS_WINDOWS
  455.     PyMem_DEL(rfd2obj);
  456.     PyMem_DEL(wfd2obj);
  457.     PyMem_DEL(efd2obj);
  458. #endif
  459.     return ret;
  460. }
  461. #endif /* !AMITCP || INET225 */
  462.  
  463. static char select_doc[] =
  464. "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
  465. \n\
  466. Wait until one or more file descriptors are ready for some kind of I/O.\n\
  467. The first three arguments are lists of file descriptors to be waited for:\n\
  468. rlist -- wait until ready for reading\n\
  469. wlist -- wait until ready for writing\n\
  470. xlist -- wait for an ``exceptional condition''\n\
  471. If only one kind of condition is required, pass [] for the other lists.\n\
  472. A file descriptor is either a socket or file object, or a small integer\n\
  473. gotten from a fileno() method call on one of those.\n\
  474. \n\
  475. The optional 4th argument specifies a timeout in seconds; it may be\n\
  476. a floating point number to specify fractions of seconds.  If it is absent\n\
  477. or None, the call will never time out.\n\
  478. \n\
  479. The return value is a tuple of three lists corresponding to the first three\n\
  480. arguments; each contains the subset of the corresponding file descriptors\n\
  481. that are ready.\n\
  482. \n\
  483. *** IMPORTANT NOTICE ***\n\
  484. On Windows, only sockets are supported; on Unix, all file descriptors.";
  485.  
  486.  
  487. static PyMethodDef select_methods[] = {
  488.     {"select",    select_select, 1, select_doc},
  489.     {0,      0},                 /* sentinel */
  490. };
  491.  
  492. static char module_doc[] =
  493. "This module supports asynchronous I/O on multiple file descriptors.\n\
  494. \n\
  495. *** IMPORTANT NOTICE ***\n\
  496. On Windows, only sockets are supported; on Unix, all file descriptors.";
  497.  
  498. DL_EXPORT(void)
  499. initselect()
  500. {
  501.     PyObject *m, *d;
  502.     m = Py_InitModule3("select", select_methods, module_doc);
  503.     d = PyModule_GetDict(m);
  504.     SelectError = PyErr_NewException("select.error", NULL, NULL);
  505.     PyDict_SetItemString(d, "error", SelectError);
  506. }
  507.